<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Db_Table_Definition - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.db.table.definition.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.db.table.definition.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.table.relationships.html">Zend_Db_Table Relationships</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.debug.html">Zend_Debug</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.db.table.definition" class="section"><div class="info"><h1 class="title">Zend_Db_Table_Definition</h1></div>
    

    <div class="section" id="zend.db.table.definition.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Db_Table_Definition</span> is a class that can be used to
            describe the relationships and configuration options that should be used when
            <span class="classname">Zend_Db_Table</span> is used via concrete instantiation.
        </p>
    </div>

    <div class="section" id="zend.db.table.definition.usage"><div class="info"><h1 class="title">Basic Usage</h1></div>
        

        <p class="para">
            For all of the same options that are available when configuring an extended
            <span class="classname">Zend_Db_Table_Abstract</span> class, those options are also available
            when describing a definition file. This definition file should be passed to the class at
            instantiation time so that it can know the full definition of all tables
            in said definition.
        </p>

        <p class="para">
            Below is a definition that will describe the table names and relationships
            between table objects. Note: if &#039;name&#039; is left out of the definition, it
            will be taken as the key of the defined table (an example of this is in the
            &#039;genre&#039; section in the example below.)
        </p>

        <div class="example" id="zend.db.table.definition.example1"><div class="info"><p><b>Example #1 Describing the Definition of a Database Data Model</b></p></div>
            

            <pre class="programlisting brush: php">
$definition = new Zend_Db_Table_Definition(array(
    &#039;author&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;author&#039;,
        &#039;dependentTables&#039; =&gt; array(&#039;book&#039;)
        ),
    &#039;book&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;book&#039;,
        &#039;referenceMap&#039; =&gt; array(
            &#039;author&#039; =&gt; array(
                &#039;columns&#039; =&gt; &#039;author_id&#039;,
                &#039;refTableClass&#039; =&gt; &#039;author&#039;,
                &#039;refColumns&#039; =&gt; &#039;id&#039;
                )
            )
        ),
    &#039;genre&#039; =&gt; null,
    &#039;book_to_genre&#039; =&gt; array(
        &#039;referenceMap&#039; =&gt; array(
            &#039;book&#039; =&gt; array(
                &#039;columns&#039; =&gt; &#039;book_id&#039;,
                &#039;refTableClass&#039; =&gt; &#039;book&#039;,
                &#039;refColumns&#039; =&gt; &#039;id&#039;
                ),
            &#039;genre&#039; =&gt; array(
                &#039;columns&#039; =&gt; &#039;genre_id&#039;,
                &#039;refTableClass&#039; =&gt; &#039;genre&#039;,
                &#039;refColumns&#039; =&gt; &#039;id&#039;
                )
            )
        )
    ));
</pre>

        </div>

        <p class="para">
            As you can see, the same options you&#039;d generally see inside of an
            extended <span class="classname">Zend_Db_Table_Abstract</span> class are documented in this
            array as well. When passed into <span class="classname">Zend_Db_Table</span> constructor, this
            definition is <em class="emphasis">persisted</em> to any tables it will need
            to create in order to return the proper rows.
        </p>

        <p class="para">
            Below is an example of the primary table instantiation as well as
            the  <span class="methodname">findDependentRowset()</span> and
             <span class="methodname">findManyToManyRowset()</span> calls that will
            correspond to the data model described above:
        </p>

        <div class="example" id="zend.db.table.definition.example2"><div class="info"><p><b>Example #2 Interacting with the described definition</b></p></div>
            

            <pre class="programlisting brush: php">
$authorTable = new Zend_Db_Table(&#039;author&#039;, $definition);
$authors = $authorTable-&gt;fetchAll();

foreach ($authors as $author) {
    echo $author-&gt;id
       . &#039;: &#039;
       . $author-&gt;first_name
       . &#039; &#039;
       . $author-&gt;last_name
       . PHP_EOL;
    $books = $author-&gt;findDependentRowset(&#039;book&#039;);
    foreach ($books as $book) {
        echo &#039;    Book: &#039; . $book-&gt;title . PHP_EOL;
        $genreOutputArray = array();
        $genres = $book-&gt;findManyToManyRowset(&#039;genre&#039;, &#039;book_to_genre&#039;);
        foreach ($genres as $genreRow) {
            $genreOutputArray[] = $genreRow-&gt;name;
        }
        echo &#039;        Genre: &#039; . implode(&#039;, &#039;, $genreOutputArray) . PHP_EOL;
    }
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.db.table.definition.advanced-usage"><div class="info"><h1 class="title">Advanced Usage</h1></div>
        

        <p class="para">
            Sometimes you want to use both paradigms for defining and using the
            table gateway: both by extension and concrete instantiation. To do this
            simply leave out any table configurations out of the definition. This will
            allow <span class="classname">Zend_Db_Table</span> to look for the actual refered class instead
            of the definition key.
        </p>

        <p class="para">
            Building on the example above, we will allow for one of the table configurations
            to be a <span class="classname">Zend_Db_Table_Abstract</span> extended class, while keeping the
            rest of the tables as part of the definition. We will also show how one would interact
            with this new definition.
        </p>

        <div class="example" id="zend.db.table.definition.example3"><div class="info"><p><b>Example #3 Interacting A Mixed Use Zend_Db_Table Definition</b></p></div>
            

            <pre class="programlisting brush: php">
class MyBook extends Zend_Db_Table_Abstract
{
    protected $_name = &#039;book&#039;;
    protected $_referenceMap = array(
        &#039;author&#039; =&gt; array(
            &#039;columns&#039; =&gt; &#039;author_id&#039;,
            &#039;refTableClass&#039; =&gt; &#039;author&#039;,
            &#039;refColumns&#039; =&gt; &#039;id&#039;
            )
        );
}

$definition = new Zend_Db_Table_Definition(array(
    &#039;author&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;author&#039;,
        &#039;dependentTables&#039; =&gt; array(&#039;MyBook&#039;)
        ),
    &#039;genre&#039; =&gt; null,
    &#039;book_to_genre&#039; =&gt; array(
        &#039;referenceMap&#039; =&gt; array(
            &#039;book&#039; =&gt; array(
                &#039;columns&#039; =&gt; &#039;book_id&#039;,
                &#039;refTableClass&#039; =&gt; &#039;MyBook&#039;,
                &#039;refColumns&#039; =&gt; &#039;id&#039;
                ),
            &#039;genre&#039; =&gt; array(
                &#039;columns&#039; =&gt; &#039;genre_id&#039;,
                &#039;refTableClass&#039; =&gt; &#039;genre&#039;,
                &#039;refColumns&#039; =&gt; &#039;id&#039;
                )
            )
        )
    ));

$authorTable = new Zend_Db_Table(&#039;author&#039;, $definition);
$authors = $authorTable-&gt;fetchAll();

foreach ($authors as $author) {
    echo $author-&gt;id
       . &#039;: &#039;
       . $author-&gt;first_name
       . &#039; &#039;
       . $author-&gt;last_name
       . PHP_EOL;
    $books = $author-&gt;findDependentRowset(new MyBook());
    foreach ($books as $book) {
        echo &#039;    Book: &#039; . $book-&gt;title . PHP_EOL;
        $genreOutputArray = array();
        $genres = $book-&gt;findManyToManyRowset(&#039;genre&#039;, &#039;book_to_genre&#039;);
        foreach ($genres as $genreRow) {
            $genreOutputArray[] = $genreRow-&gt;name;
        }
        echo &#039;        Genre: &#039; . implode(&#039;, &#039;, $genreOutputArray) . PHP_EOL;
    }
}
</pre>

        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.table.relationships.html">Zend_Db_Table Relationships</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.debug.html">Zend_Debug</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.db.html">Zend_Db</a></li>
  <li><a href="zend.db.adapter.html">Zend_Db_Adapter</a></li>
  <li><a href="zend.db.statement.html">Zend_Db_Statement</a></li>
  <li><a href="zend.db.profiler.html">Zend_Db_Profiler</a></li>
  <li><a href="zend.db.select.html">Zend_Db_Select</a></li>
  <li><a href="zend.db.table.html">Zend_Db_Table</a></li>
  <li><a href="zend.db.table.row.html">Zend_Db_Table_Row</a></li>
  <li><a href="zend.db.table.rowset.html">Zend_Db_Table_Rowset</a></li>
  <li><a href="zend.db.table.relationships.html">Zend_Db_Table Relationships</a></li>
  <li class="active"><a href="zend.db.table.definition.html">Zend_Db_Table_Definition</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>